<template>
	<view>
		<view style=" display: flex;justify-content: center;
		background-color: #CFEAF1;height: 44px;width: 100%;align-items: center;
		position: sticky;top: 0;z-index: 1;">
			<image @click="noreg()" src="../../static/back.png" style="height: 30px;width: 30px;
		position: absolute;left: 10px;"></image>
			<h3>注册</h3>
		</view>
	<view class="container">
		<uni-forms ref="form" :rules="{}" style="margin-top: 20px;">
			<uni-forms-item label="头像" style="margin-bottom: 30px;">
				<image :src="image" @click="img()" style="width: 50px;
		height: 50px;display: block;margin: 0 auto;position: fixed;right: 30px;border-radius: 80%;"></image>
			</uni-forms-item>
			<uni-forms-item label="用户名" label-width="70px">
				<uni-easyinput v-model="users.username" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码" label-width="70px">
				<uni-easyinput v-model="users.password" placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item label="电话号码" label-width="70px">
				<uni-easyinput  v-model="users.phone" placeholder="请输入电话号码"/>
			</uni-forms-item>
			<uni-forms-item label="收货地址" label-width="70px">
				<view class="" style="display: flex;height: 35px;">
					<uni-easyinput v-model="users.address" placeholder="请输入收货地址" style="width: 100px;" />
			    <uni-icons type="paperplane-filled" size="25px" style="line-height: 35px;margin-left: 10px;"></uni-icons>
				</view>
			</uni-forms-item>
			<uni-forms-item label="个性签名" label-width="70px">
				<uni-easyinput v-model="users.signature" placeholder="请输入个性签名" />
				
			</uni-forms-item>
			<uni-forms-item>
					<button v-show="regok" type="primary" @click="wureg()" style="width: 80%;">注  册</button>
					<button v-show="!regok" type="primary" @click="reg()" style="width: 80%;">注  册</button>
				
			</uni-forms-item>
		</uni-forms>
	</view>
	</view>
</template>

<script>
import { pathToBase64 } from 'js_sdk/mmmm-image-tools/index.js'
	export default {
		data() {
			return {
				image:'../../static/nolog.png',
				users:{},
				alluser:[],
				regok:true,
				regyes:true
			}
		},
		methods: {
			
			noreg(){
				uni.navigateTo({
					url:'/pages/login/login'
				})
			},
			wureg(){
				
				if(!this.users.username){
					uni.showToast({
						title: '用户名不能为空',
						icon: 'none'
					});
				}
				else if(!this.users.password){
					uni.showToast({
						title: '密码不能为空',
						icon: 'none'
					});
				}else if(!this.users.phone){
					uni.showToast({
						title: '手机号码不能为空',
						icon: 'none'
					});
					
				}else if(!this.users.address){
					uni.showToast({
						title: '地址不能为空',
						icon: 'none'
					});
					
				}else{
					uni.request({
						url: 'http://localhost:8484/ttwm/ttwm/addUser',
						method: 'POST',
						data: this.users,
						success: res => {
							if(res.data.success){
								uni.showToast({
									title: '成功',
									icon: 'none'
								});
								uni.navigateTo({
									url:"/pages/login/login"
								})
							}else{
								uni.showToast({
									title: '失败',
									icon: 'none'
								});
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}
			},
			reg(){
				
				
				if(!this.users.username){
					uni.showToast({
						title: '用户名不能为空',
						icon: 'none'
					});
					
				}else if(!this.users.password){
					uni.showToast({
						title: '密码不能为空',
						icon: 'none'
					});
					
				}else if(!/^1[0-9]{10}$/.test(this.users.phone)){
					uni.showToast({
						title: '手机号码格式不正确',
						icon: 'none'
					});
					
				}
				else if(!this.users.address){
					uni.showToast({
						title: '地址不能为空',
						icon: 'none'
					});
					
				}else{
				for (var i = 0; i < this.alluser.length; i++) {
					if(this.users.username == this.alluser[i].username){
					uni.showToast({
						title: '该用户名已被占用，请重新输入',
						icon: 'none'
					});
					this.regyes=true
					break
				}else if(this.users.phone == this.alluser[i].phone){
					uni.showToast({
						title: '该手机号已注册，一个手机号只能注册一个账号',
						icon: 'none',
						duration:3000
					});
					this.regyes=true
					break
				}else{
					this.regyes=false
				}
				}
				}
				
			console.log(this.regyes);
			if(this.regyes===false){
					uni.request({
						url: 'http://localhost:8484/ttwm/ttwm/addUser',
						method: 'POST',
						data: this.users,
						success: res => {
							if(res.data.success){
								uni.showToast({
									title: '注册成功',
									icon: 'none'
								});
								uni.navigateTo({
									url:"/pages/login/login"
								})
							}else{
								uni.showToast({
									title: '失败',
									icon: 'none'
								});
							}
						},
						fail: () => {},
						complete: () => {}
					});
					
				}
			},
			img(){
				uni.chooseImage({
					count:1,
					sizeType:["original","compressed"],
					sourceType:["alcum","camera"],
					success:res=>{
						uni.getImageInfo({
							src:res.tempFilePaths[0],
							success:(path)=>{
								pathToBase64(path.path).then(base64=>{
									this.image=base64
									this.users.chooseImage=base64
								}).catch(error=>{
									console.log(error);
								})
							}
						})
					}
				})
				}
		},
		onLoad() {
			
			uni.request({
				url: 'http://localhost:8484/ttwm/ttwm/allUser',
				method: 'POST',
				data: {},
				success: res => {
					if(res.data.success){
						this.regok=false
						// console.log(this.regok);
						this.alluser=res.data.list
						// console.log(this.alluser);
					}else{
						this.regok=true
					}
				},
				fail: () => {},
				complete: () => {}
				
			});
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
		padding-top: 60px;
	}

</style>
